package com.lagou.wwyan.rpc.client;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class DefaultRpcClientSelector implements RpcClientSelector{
    private Random random = new Random();
    @Override
    public RpcClient select(String service, Map<String, List<RpcClient>> clientPool) {
        List<RpcClient> rpcClients = clientPool.get(service);
        if (rpcClients == null || rpcClients.isEmpty()){
            return null;
        }
        if (rpcClients.size() ==1){
            return rpcClients.get(0);
        }
        int index = random.nextInt(rpcClients.size());
        return rpcClients.get(index);
    }
}
